﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm2
{
    public partial class DbForm2 : Form
    {
        public DbForm2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            var sqlStu = "select * from Student";
            var dt = DBhelp.GetDataTable(sqlStu);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }


        //查找
        private void button4_Click(object sender, EventArgs e)
        {
            var name = DbNameTxt.Text;
            var age = DbAgeTxt.Text;
            var score = DbScore.Text;
            if(name!="" && age == "" && score == "")
            {
                var sql = string.Format("select * from Student where StudentName like '%{0}%'", name);
                var dt = DBhelp.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else if (age!="" && score == "" && name == "")
            {
                var sql = string.Format("select * from Student where StudentName like '%{0}%'", age);
                var dt = DBhelp.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else if (score!="" && name == "" && age == "")
            {
                var sql = string.Format("select * from Student where StudentName like '%{0}%'", score);
                var dt = DBhelp.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else if (name != "" && age != "" && score != "")
            {
                var sql = string.Format("select * from Student where StudentName like '%{0}%'", name);
                var dt = DBhelp.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("请输入你需要查找数据的相关内容","提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            }
            
           
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //添加
        private void button1_Click(object sender, EventArgs e)
        {
            var name = DbNameTxt.Text;
            var age = DbAgeTxt.Text;
            var sql = string.Format("insert into Student(StudentName, Age) values('{0}', '{1}')",name,age);
            var dt = DBhelp.GetDataTable(sql);
            var sqlStu = "select * from Student";
            var h1 = DBhelp.GetDataTable(sqlStu);
            dataGridView1.DataSource = dt;
        }


        //删除
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var row = dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var res = MessageBox.Show("确定删除吗!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Student where Id={0}", row);
                    var rowCout = DBhelp.Update(sql);
                    if (rowCout>0)
                    {
                        var sqlStu = "select * from Student";
                        var dt = DBhelp.GetDataTable(sql);
                        dataGridView1.DataSource = dt;
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何的行","提示");
            }
        }

        //更改
        private void button3_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["StudentName"].Value;
                var age = (string)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var score = (string)dataGridView1.SelectedRows[0].Cells["Score"].Value;
                ChangeForm changeForm = new ChangeForm(id, name, age, score);
                changeForm.Show();

            }
            else
            {
                MessageBox.Show("请选择更改的行", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            }
          
         
           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }


    

}
